<?php

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/10
 * Time: 15:19
 */

namespace App\Http\Controllers\Websocket;

use App\Http\Controllers\Controller;
use App\Http\Controllers\LibApi\CqLibApiSip2Controller;
use App\Http\Controllers\LibApi\CqLibSip2Controller;
use Illuminate\Support\Facades\Log;

/**
 * sip2  数据解析（socket）  重庆市图书馆
 * Class ScoketDataAnalysis
 * @package app\websocket\controller
 */
class SocketClientCQSip2  extends Controller
{
    public $service_ip = ''; //socket ip      涪陵区图书馆
    public $service_port = ''; //socket 端口    30001

    public function __construct($server_ip = null, $server_port = null)
    {
        parent::__construct();

        $this->service_ip = $server_ip;
        $this->service_port = $server_port;
    }


    /**
     * socket 作为客户端  获取数据
     *
     * $command = "9120191009     101010AO|AA00010160125000|XO|XK0|CR\r";  //发送的指令  发送指令前必须先发送 93 指令进行登录
     */
    public function socketClientGetData($command)
    {
        try {
            //创建 TCP/IP socket
            $socket = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if ($socket < 0) {
                throw new \Exception("socket创建失败原因: " . socket_strerror($socket));
            }
            $result = socket_connect($socket, $this->service_ip, $this->service_port);
            if ($result < 0) {
                throw new \Exception("SOCKET连接失败原因: ($result) " . socket_strerror($result));
            }
            //先进行登录，必须是同一个 socket 套接字
            $libSip2Obj = new CqLibApiSip2Controller();
            $login_command = $libSip2Obj->RFIDLogin();
            socket_write($socket, $login_command, strlen($login_command));
            // while ($out = socket_read($socket, 2048)) {
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0)); //socket_read 超时，限制发送超时时间 2 秒
            socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 5, "usec" => 0)); //socket_read 超时，限制接收超时时间 5 秒

            $data = socket_read($socket, 8192); //4098
            $data = trim($data); //2023.6.12 对接贵阳市图书馆新增

            $three = $data{
                2}; //返回值   94后面1表示成功，0表示失败
            if ($three !== '1') {
                //写入错误日志
                Log::channel('libapi')->info($login_command);
                //写入错误日志
                Log::channel('libapi')->info($data);

                throw new \Exception("SIP2登录失败");
            }

            socket_write($socket, $command, strlen($command));
            // while ($out = socket_read($socket, 2048)) {
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0)); //socket_read 超时，限制发送超时时间 2 秒
            socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 30, "usec" => 0)); //socket_read 超时，限制接收超时时间 5 秒

            $data = socket_read($socket, 81920); //4098
            $data = trim($data); //2023.6.12 对接贵阳市少儿图书馆新增
            socket_close($socket);
            if ($data) {
                return ['code' => 200, 'msg' => '获取成功', 'content' => $data];
            }
            return ['code' => 202, 'msg' => '数据获取失败'];
        } catch (\Exception $e) {
            Log::channel('libapi')->info('重庆市图书馆SIP2接口处理失败-' . $e->getMessage() . $e->getFile() . $e->getCode());
            return ['code' => 203, 'msg' => '服务器连接失败'];
        }
    }
}

